/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admin;

/**
 *
 * @author BAILLEURS SERVICES
 */

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class WriteFile {

    private String fileText;
    boolean writeToFile;
    private Hashtable errors;
    private String selectFile;
    private String url;
    private String url1;
    private String selectPosition;

    public WriteFile() {
            selectPosition="";
            selectFile="";
            fileText="";
            errors = new Hashtable();
            url="";
            url1="";
    }

     public boolean validate() {


        boolean allOk = true;

    /*    if (getFileText().equals("")) {
            errors.put("fileText"," inserer code ici");
            allOk = false;
        } */
        if (selectFile.equals("choix")) {
            errors.put("selectFile","Choix file !");
            allOk =false;
        }

        if (selectFile.equals("ACC")&&!getFileText().equals("")) {

                this.url="d:/hieu/ACC/";
                allOk =true;
        }


       if (selectFile.equals("ABN")&&!getFileText().equals("")) {

            this.url="d:/hieu/ABN/";
               allOk =true;
        }
        if (selectFile.equals("VST")&&!getFileText().equals("")) {

               this.url="d:/hieu/VST/";
               allOk =true;
        }
        if (selectFile.equals("PUB")&&!getFileText().equals("")) {
            this.url="d:/hieu/PUB/";
               allOk =true;
        }

         if (selectPosition.equals("1")) {
                this.url1="1.txt";
                allOk =true;
        }
         if (selectPosition.equals("2")) {
                this.url1="2.txt";
                allOk =true;
        }
         if (selectPosition.equals("3")) {
                this.url1="3.txt";
                allOk =true;
        }
         if (selectPosition.equals("4")) {
                this.url1="4.txt";
                allOk =true;
        }
        return allOk;
     }

     public String getErrorMsg(String s) {
        String errorMsg = (String) getErrors().get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

      public Hashtable getErrors() {
        return errors;
    }

    /**
     * @param errors the errors to set
     */
    public void setErrors(Hashtable errors) {
        this.errors = errors;

    }
    public String getFileText() {
        return fileText;
    }
    public void setFileText(String fileText) {
        this.fileText = fileText;
    }
    public boolean getWriteToFile() {
        try {
                 String path="";
                 path=this.url+this.url1;
                 System.out.print("duong dan save file"+path);
                 BufferedWriter out = new BufferedWriter(new FileWriter(path));
                 out.write(this.getFileText());
                 out.close();
                writeToFile = true;

        } catch (IOException e) {
            writeToFile = false;
            //Log the exception here
            e.printStackTrace();
        }
        return writeToFile;
    }
    public void setWriteToFile(boolean writeToFile) {
        this.writeToFile = writeToFile;
    }

    /**
     * @return the selectFile
     */
    public String getSelectFile() {
        return selectFile;
    }

    /**
     * @param selectFile the selectFile to set
     */
    public void setSelectFile(String selectFile) {
        this.selectFile = selectFile;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }


    public String getStringFromFile(String Page,String Position){

        StringBuffer buffer =new StringBuffer();
        try {
                String path="d:/hieu/";
                path=path+Page+"/"+Position+".txt";
                System.out.print(" DDDDDDDDDDDDDDD duong dan="+path);
                BufferedReader input = new BufferedReader(new FileReader(path));

                String line2 = "";
                while ((line2 = input.readLine())!= null) {
                   buffer.append(line2.toString());
         }
                System.out.print("String "+buffer.toString());
        }catch(IOException e){
             e.printStackTrace();
        }
       System.out.print("hheehe" +buffer.toString());
       return buffer.toString();

}

    /**
     * @return the selectPosition
     */
    public String getSelectPosition() {
        return selectPosition;
    }

    /**
     * @param selectPosition the selectPosition to set
     */
    public void setSelectPosition(String selectPosition) {
        this.selectPosition = selectPosition;
    }

    /**
     * @return the url1
     */
    public String getUrl1() {
        return url1;
    }

    /**
     * @param url1 the url1 to set
     */
    public void setUrl1(String url1) {
        this.url1 = url1;
    }
}


